package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.dish_pojo.Inventory;
import org.example.dish_pojo.Result;
import org.example.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("kucun")
public class KucunCaiPinController {
    @Autowired
    private InventoryService service;

    // 添加库存
    @RequestMapping("/addInventory")
    public Result addInventory(@RequestBody Inventory inventory) {
        boolean save = service.save(inventory);
        if (!save) {
            return new Result(500, "添加失败", null);
        }
        return new Result(200, "添加成功", null);
    }

    //修改库存
    @RequestMapping("/updateInventory")
    public Result updateInventory(@RequestBody Inventory inventory) {
        boolean update = service.updateById(inventory);
        if (!update) {
            return new Result(500, "修改失败", null);
        }
        return new Result(200, "修改成功", null);
    }

    // 删除库存,根据dishId删除
    // 删除库存,根据dishId删除
    @RequestMapping("/deleteInventory")
    public Result deleteInventory(Integer dishId) {
        // 根据dishId删除库存记录，而不是使用removeById方法
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id", dishId);
        boolean delete = service.remove(queryWrapper);
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }
}
